﻿using System;
using System.IO;

namespace Helper
{
    public static class TraceLog
    {
        public static void Print(string format, params object[] args)
        {
            string msg = GetMessage(format, args);
            DateTime dt = System.DateTime.Now.ToLocalTime();

            ConsoleColor color = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Gray;
            if (_newLine)
            {
                string str = string.Format("[{0}]: ", dt);
                Console.Write(str);
                WriteToLogFile(str);
                _newLine = false;
            }
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(msg);
            WriteToLogFile(msg);
            Console.ForegroundColor = color;
        }

        public static void PrintLn(string format, params object[] args)
        {
            Print(format, args);
            WriteToLogFile("\r\n");
            Console.WriteLine();
            _newLine = true;
        }

        public static void Error(string format, params object[] args)
        {
            string msg = GetMessage(format, args);
            DateTime dt = System.DateTime.Now.ToLocalTime();

            ConsoleColor color = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Red;
            if (!_newLine)
            {
                WriteToLogFile("\r\n");
                Console.WriteLine();
            }
            string str = string.Format("[{0}]: {1}", dt, msg);
            WriteToLogFile(str + "\r\n");
            Console.WriteLine(str);
            Console.ForegroundColor = color;
            _newLine = true;
        }

        private static string GetMessage(string format, object[] args)
        {
            if (args.Length > 0)
            {
                return string.Format(format, args);
            }
            else
            {
                return format;
            }
        }

        private static void WriteToLogFile(string str)
        {
            if (!WriteToFile)
            {
                return;
            }
            if (string.IsNullOrEmpty(str))
            {
                return;
            }

            str = str.Replace("\b", "");
            StreamWriter sw = new StreamWriter(c_LogFile, true);
            sw.Write(str);
            sw.Flush();
            sw.Close();
        }

        public const string c_LogFile = "log.txt";
        private static bool _newLine = true;
        public static bool WriteToFile = false;
    }
}
